<template>
  <AmisFormComponent
    :formContext="amisFormContextObj"
    :formData="props.amisFormDataObj"
    :hideAmisButton="amisFormContextObj.hideAmisButton"
    :readOnlyAmis="amisFormContextObj.readOnlyAmis"
    :fieldsPermission="props.fieldsPermission"
    ref="amisFormComponent"
  />
</template>

<script lang="ts" setup>
import * as FormApi from '@/api/bpm/form'
import {string} from "vue-types";
import {AmisFormContext} from "@/components/AmisFormComponent/amisFormContext";
import {getGetParams} from "@/utils/url";
import {parseBoolean} from "@/utils/boolean";


defineOptions({name: 'AmisFormShow'})

const route = useRoute() // 路由信息
const schema = ref()
const amisFormContextObj = ref<AmisFormContext>({}) // amisForm 的 上下文数据


console.log("route-AmisFormShow",route)


const props = defineProps({
  readOnlyAmis: {
    type: Boolean,
    required:false,
    default: () => {
      return false
    }
  },
  hideAmisButton: {
    type: Boolean,
    required:false,
    default: () => {
      return false
    }
  },
  amisFormId: {
    type: string,
    default: () => {
      return null
    }
  },
  amisFormDataObj: {
    type: Object ,
    required: false,
    default: ()=>{
      return {};
    }
  },
  fieldsPermission: {
    type: Object ,
    required: false,
    default: ()=>{
      return {};
    }
  }
})

const amisFormComponent = ref(null)

/** 初始化 **/
onBeforeMount(async () => {
  console.log("onMounted...")
  // 上层组件传递过来的
  if(!!props.amisFormId){
    amisFormContextObj.value.amisFormId = props.amisFormId
    amisFormContextObj.value.readOnlyAmis = props.readOnlyAmis
    amisFormContextObj.value.hideAmisButton = props.hideAmisButton
  }
  // 路由元数据
  else if(!!route.meta.query?.amisFormId){
    amisFormContextObj.value.amisFormId = route.meta.query.amisFormId
    amisFormContextObj.value.readOnlyAmis = route.meta.query.readOnlyAmis
    amisFormContextObj.value.hideAmisButton = route.meta.query.hideAmisButton
  }
  // url上获取
  else {
    let getParams = getGetParams(location.href);
    amisFormContextObj.value.amisFormId = getParams.get("amisFormId")
    amisFormContextObj.value.readOnlyAmis = getParams.get("readOnlyAmis")
    amisFormContextObj.value.hideAmisButton = getParams.get("hideAmisButton")
  }

  amisFormContextObj.value.readOnlyAmis = parseBoolean(amisFormContextObj.value.readOnlyAmis)
  amisFormContextObj.value.hideAmisButton = parseBoolean(amisFormContextObj.value.hideAmisButton)

  if(props.readOnlyAmis){
    amisFormContextObj.value.readOnlyAmis = props.readOnlyAmis
  }
  if(props.hideAmisButton){
    amisFormContextObj.value.hideAmisButton = props.hideAmisButton
  }

  console.log(amisFormContextObj.value,'amisFormContextObj')
})
onActivated(()=>{
  console.log("onActivated...")
  let formData = getFormDate()
  valiDate().then(valiRes=>{
    console.log("校验结果",valiRes)
  })
  console.log("当前表单数据",formData)
})
onUnmounted(async () => {
  console.log("onUnmounted...")
  amisFormComponent.value?.onAmisUnmounted()
})
const getFormDate = function (){
  console.log("getFormDate...")
  let formData = amisFormComponent.value?.getFormDate()
  console.log("当前表单数据",formData)
  return formData ;
}

const valiDate = async function(){
  console.log("validate...")
  let validate = await amisFormComponent.value?.valiDate()
  console.log("校验结果",validate)
  return validate ;
}

const setData = function(value: any, replace?: boolean){
  console.log("setData...")
  amisFormComponent.value?.setData(value, replace)
}

const onAmisUnmounted = function(){
  console.log("onAmisUnmounted...")
  amisFormComponent.value?.onAmisUnmounted()
}

// 暴露方法，提供给父组件
defineExpose({getFormDate,setData,valiDate,onAmisUnmounted})

</script>
